/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.pnp.servlets;

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.HashMap;
import javax.servlet.*;
import javax.servlet.http.*;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import pe.gob.pnp.factories.*;

/**

/**
 *
 * @author Home
 */
public class ReporteProduccionIndexados extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String fechaInicio = request.getParameter("fechaInicio");
        String fechaFin = request.getParameter("fechaFin");
        String usuario = request.getParameter("usuario");
        String formato = "dd/MM/yyyy";
        response.setContentType("application/pdf");
        ServletOutputStream out = response.getOutputStream();
        Connection conn = null;
        try {
            Date dFechaInicio = new Date(new SimpleDateFormat(formato).parse(fechaInicio).getTime());
            Date dFechaFin = new Date(new SimpleDateFormat(formato).parse(fechaFin).getTime());
            HashMap<String, Object> parametros = new HashMap<String, Object>();
            parametros.put("P_FECHAINICIO", dFechaInicio);
            parametros.put("P_FECHAFIN", dFechaFin);
            parametros.put("P_USUAIDENTIFICADOR", usuario);
            String rutareportes = getServletContext().getRealPath("/WEB-INF/reportes");
            File reporte = new File(rutareportes + "/rptProduccionIndexados.jasper");
            FileInputStream in = new FileInputStream(reporte);
            conn = ConnectionPool.obtenerConexion();
            byte[] bytes = JasperRunManager.runReportToPdf(in, parametros, conn);
            out.write(bytes, 0, bytes.length);
            out.flush();
            out.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new ServletException(ex);
        } catch (JRException ex) {
            ex.printStackTrace();
            throw new ServletException(ex);
        } catch (ParseException ex) {
            ex.printStackTrace();
        } finally {
            out.close();
            ConnectionPool.liberarConexion(conn);
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
